/**
 * @Author: shaochong
 * @Date: 2020/12/17
 * @Description:
 */
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class TestHmac {
    public static void main(String[] args) throws Exception {
        // 示例sk
        String sk = "HWHp9xFVlbboxIU2S6DHA7sf9sGzt3";
        // 示例要加密的message(ak+timestamp+nonce)
        String message = "20001031608119594123221";

        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(sk.getBytes(), "HmacSHA256"));
        // hmac256加密,得到二进制的加密结果
        byte[] bytes = mac.doFinal(message.getBytes());
        // 对二进制密文进行base64,得到sign
        final BASE64Encoder encoder = new BASE64Encoder();
        String m7sign = encoder.encode(bytes);
        System.out.println(m7sign);
    }
}
